//
//  BOLL.m
//  AppFinance
//
//  Created by wei on 12-7-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFBOLL.h"

@implementation AFBOLL

static AFBOLL *_boll = nil;

static float _kpiMaxBoll = 0;
static float _kpiMinBoll = 100000000;

static float _kpiLastAverage = 0;
static float _kpiLastUp = 0;
static float _kpiLastDown = 0;

+(AFBOLL*) app{
    if (_boll == nil) {
        _boll = [[self alloc] init];
    }
    return _boll;
}
+(float) kpiMaxBoll{
    return _kpiMaxBoll;
}
+(float) kpiMinBoll{
    return _kpiMinBoll;
}

+(float) kpiLastAverage{
    return _kpiLastAverage;
}
+(float) kpiLastUp{
    return _kpiLastUp;
}
+(float) kpiLastDown{
    return _kpiLastDown;
}

int boll_iParam[] = {10};

-(void) initKlineArray:(NSArray *)_kLineArray{
    kLineArray = _kLineArray;
    
    _kpiMaxBoll = 0;
    _kpiMinBoll = 100000000;
    
    _averageArray = nil;
    _upArray = nil;
    _downArray = nil;
}

-(void) bollData:(int) day drawCount:(int)_drawCount{

    if(nil == kLineArray || [kLineArray count] <= 0){
        return;
    }
    int n = day;
    if(n > [kLineArray count] || n < 1 || (n + n) - 2 >= [kLineArray count]){
        return;
    }

    float sum = 0.0;
    
    // 平均值
    _averageArray = [NSMutableArray array];
    [self averageClose:day];
    
    // 上线 和 下线
    _upArray = [NSMutableArray array];
    _downArray = [NSMutableArray array];
    
    NSDictionary *dict;
    float close = 0;
    for(int i = n - 1; i < (n + n) - 2; i++) {
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        float value = close - [[_averageArray objectAtIndex:i] floatValue];
        sum += value * value;
    }
    
    for (int i = 0; i < (n + n) - 2; i++) {
        [_upArray addObject:[NSNumber numberWithFloat:0]];
        [_downArray addObject:[NSNumber numberWithFloat:0]];
    }
    
    float prevalue = 0.0;
    float up = 0.0;
    float down = 0.0;
    
    int _kLineCount = (int)[kLineArray count];
    for(int i = (n + n) - 2; i < _kLineCount; i++) {
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];

        sum -= prevalue;
        float value = close - [[_averageArray objectAtIndex:i] floatValue];
        sum += value * value;
        value = sqrtf(sum / (float)n) * 1.805f;
        up = [[_averageArray objectAtIndex:i] floatValue] + value;
        down = [[_averageArray objectAtIndex:i] floatValue] - value;
        [_upArray addObject:[NSNumber numberWithFloat:up]];
        [_downArray addObject:[NSNumber numberWithFloat:down]];
        
        if (_kpiMaxBoll < up) {
            _kpiMaxBoll = up;
        }
        if (_kpiMinBoll > down) {
            _kpiMinBoll = down;
        }

        dict = [kLineArray objectAtIndex:((i - n) + 1)];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        value = close - [[_averageArray objectAtIndex:((i - n) + 1)] floatValue];
        prevalue = value * value;
        
        if (i == (_kLineCount - 1)) {
            _kpiLastUp = up;
            _kpiLastDown = down;
        }
    }
}

-(void) averageClose:(int) iParam{
    if(kLineArray ==nil || [kLineArray count]== 0){
        return;
    }
    int n = iParam;
    if(n > [kLineArray count] || n < 1){
        return;
    }
    
    _averageArray = [NSMutableArray array];
    
    double sum = 0.0;
    NSDictionary *dict;
    float close = 0;
    
    for(int i = 0; i < n - 1; i++){
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        sum += close;
        
        [_averageArray addObject:[NSNumber numberWithFloat:0]];
    }
    
    float pre_close = 0;
    float _average = 0;
    for(int i = n - 1; i < [kLineArray count]; i++) {
        dict = [kLineArray objectAtIndex:i];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        sum -= pre_close;
        sum += close;
        _average = (float)(sum / (double)n);
        [_averageArray addObject:[NSNumber numberWithFloat:_average]];

        dict = [kLineArray objectAtIndex:((i - n) + 1)];
        pre_close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        //NSLog(@" _averageArray = %f",_average);
        
        if (i == ([kLineArray count] - 1)) {
            _kpiLastAverage = _average;
        }
    }
}


-(NSMutableArray *) kaverageArray{
    return _averageArray;
}
-(NSMutableArray *) kupArray{
    return _upArray;
}
-(NSMutableArray *) kdownArray{
    return _downArray;
}
@end
